<?php
include __DIR__.'/common1.php';



function put_stop($output){
    file_put_contents('test/a.php',$output);
    die;
}

/** info: 获取文件中php语法结构的变量
 * @param $code_str
 * @return array
 */
function getPhpAllVar($code_str){
    $var_name = [];
    $tokens = token_get_all($code_str);
    foreach ($tokens as $key => $value) {
        if(is_array($value)){
            $token_name = token_name($value[0]);
            switch ($token_name) {
                case 'T_VARIABLE':
                    //变量
                    preg_match('/\$([\s\S]*)/',$value[1],$pat_array);
                    //变量名
                    $var_name[] = $pat_array[1];
                    break;
            }
        }
    }
    $var_name = array_unique($var_name);

    return $var_name;
}

/** info: 将十六进制数，转化成十进制
 * @param $code_str
 */
function code_hexdec(&$code_str){
    $tokens = token_get_all($code_str);
    foreach ($tokens as $key => $value) {
        if(is_array($value)){
            $token_name = token_name($value[0]);
            switch ($token_name) {
                case 'T_LNUMBER':

                    $number = (int) hexdec($value[1]);
                    $code_str = str_replace($value[1],$number,$code_str);
                    break;
            }
        }
    }
}



function getphpCode(&$output,&$allVar,&$ERR_String)
{
    $tokens = token_get_all($output);
    //文件中所有的变量名
    $number = [];
    $number2 = [];

    $allVar = [];
    foreach ($tokens as $key => $value) {
        if (is_string($value)) {
            $tokens[$key] = $value;

        } else {
            $token_name = token_name($value[0]);
            $tokens[$key][0] = token_name($value[0]);

            switch ($token_name) {
                case 'T_CONSTANT_ENCAPSED_STRING':
                    //无法解析的字符串
                    $ERR_String[] = $value[1];
                    break;
                case 'T_LNUMBER':

                    $tokens[$key][1] = (int)hexdec($value[1]);

                    break;
                case 'T_STRING':

                    //去掉代码注释;
                    if($tokens[$key-1] == ';' && $tokens[$key+1] == ';'){
                        unset($tokens[$key]);
                        unset($tokens[$key+1]);
                    }

                    break;
                case 'T_NS_SEPARATOR':
                    //不需要转义
                    break;
                case 'T_VARIABLE':
                    //更换变量名

                    $encode = mb_detect_encoding($value[1], array("ASCII",'BIG5','UTF-8','CP936'));

                    //如果没有将该变量加入数组,便加入
                    if($encode !=='ASCII'){
                        if(!isset($allVar[$value[1]])){
                            //序号
                            $allVar[$value[1]] = count($allVar)+1;
                            //替换变量名称
                        }
                        $tokens[$key][1] = '$var_'.$allVar[$value[1]];
                    }else{
                        $number[] = ord($value[1]);
                    }
                    break;
                default:
                    //$tokens[$key][1] = addslashes($value[1]);

                    break;
            }
        }
    }



    ob_start();
    foreach ($tokens as $key => $value) {
        if (is_string($value)) {
            echo $value;
        } else {
            echo $value[1];
        }
    }

    $output = ob_get_clean();


}


function key_merge($arr1, $arr2)
{

    foreach ($arr2 as $key => $value) {

        if (isset($arr1[$key])) {
            if ($value) {
                $arr1[$key] = $value;
            } else {
                $arr1[$key] = '0';
            }
        } else {
            $arr1[$key] = $value;
        }

    }
    return $arr1;
}


//将数组格式化成php语法输出字符串
function array_format_echo($data)
{
    if (!is_array($data)) {

        return "'".addcslashes($data,'\'\\')."'";
    }
    $return = '';

    if (version_compare(PHP_VERSION, '5.5', '<')) {
        $array_format_start = 'array(' . PHP_EOL;
        $array_format_end = ')';
    } else {
        $array_format_start = '[' . PHP_EOL;
        $array_format_end = ']';
    }
    $return .= $array_format_start;
    foreach ($data as $key => $value) {
        if (is_array($value)) {
            $value = array_format_echo($value);
        } elseif (is_string($value)) {
            $value = "'".addslashes($value)."'";
        }
        $return .= "'$key' => $value," . PHP_EOL;
    }
    $return .= $array_format_end;

    return $return;
}







/** info：解析需要gzinflate解密的程序
 * @param $str
 * @param int $type
 * @return bool|mixed|string
 */
function gzinflate_decode($str,$type=1){
    $str = stripslashes($str);
    $result = @gzinflate(substr($str,10,-8));
    //halt($str);
    if($result){
        return $result;
    }else{
        $str = trim($str,'\'');
        $str = addslashes($str);
        $str = <<<EOL
<?php return gzinflate(substr('$str',10,-8));
EOL;
        $filepath = tempnam('test','bk');
        file_put_contents($filepath,$str);
        $result = @include $filepath;
        if($result){
            unlink($filepath);
            return $result;
        }else{
            return false;
        }
    }

}

/** info:  将匹配的explode('xx',xxxx);格式的直接返回数据
 * @param $str
 * @return bool|mixed
 */
function return_explode($str){
    $str = <<<EOL
<?php return $str;
EOL;
    $filepath = tempnam('test','bk');
    file_put_contents($filepath,$str);
    $result = @include $filepath;
    if($result){
        unlink($filepath);
        return $result;
    }else{
        return false;
    }
}

/** info：解析从sql文件中读取的字符串，返回需要执行的所有sql语句数组
 * @param $str
 * @return array
 */
function explodeSqlFile($str){
    $sqlArr = [];
    $str = preg_replace('/--.*/i', '', $str);
    $str = preg_replace('/\/\*.*\*\/(\;)?/i', '', $str);
    /** 去除空格 创建数组 */
    $str = explode(";\n", $str);
    foreach ($str as $v)
    {
        $v = trim($v);
        if (empty($v))
        {
            continue;
        }
        else
        {
            $sqlArr[] = $v;
        }
    }
    return $sqlArr;
}


